package com.sailing.xjpb.ipaas.client.core.repository;

import com.sailing.xjpb.common.exception.IpaasClientException;
import com.sailing.xjpb.common.web.ResponseCode;
import com.sailing.xjpb.ipaas.client.core.IpaasHttpClient;
import com.sailing.xjpb.ipaas.client.entity.dto.SipRoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.ResourceAccessException;

import java.util.HashMap;
import java.util.Map;

/**
 * 查询角色相关的接口
 *
 * @author YaoWei
 */
public class SipRoleRepository {

    @Autowired
    private IpaasHttpClient ipaasHttpClient;

    /**
     * 根据角色code查找角色信息
     *
     * @param roleCode 角色code
     */
    public SipRoleDTO findByRoleCode(String roleCode) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("roleCode", roleCode);
        String requestURI = "/external-interface/role/findRoleByCode/{roleCode}";
        return this.getRole(requestURI, paramMap);
    }


    private SipRoleDTO getRole(String url, Map<String, Object> params) {
        try {
            return ipaasHttpClient.exchange(url, HttpMethod.GET, SipRoleDTO.class, params);
        } catch (ResourceAccessException e) {
            throw parseException(e);
        }
    }

    private IpaasClientException parseException(Exception e) {
        return new IpaasClientException(ResponseCode.T_4000, "ipaas接口连接异常！", e);
    }

}
